<h2> Editing questionnaire <%= @questionnaire.id %></h2>
<%= error_messages_for 'question' %>
<div id=edit_question>
  <% form_for :questionnaire, :url => questionnaire_path(@questionnaire), :html => {:method => 'put'} do |f| %>
    <p> Name: <%= f.text_field :name %>             </p>
    <p> Maximum time: 
      <span id=timed_questionnaire_checkbox>
        <% checked = @questionnaire.max_time > 0 %>
        Timed? <%= check_box_tag :timed_questionnaire, 1, checked, :onclick => "toggle_and_reset('questionnaire_hours'); toggle_and_reset('questionnaire_minutes');" %> 
      </span>
      <%= text_field_tag 'questionnaire[hours]', @max_hours, :disabled => !checked  %> hours 
      <%= text_field_tag 'questionnaire[minutes]', @max_minutes, :disabled => !checked %> minutes
    </p>
    <p>
      <% if @questionnaire.wrong_answer_penalty == -1 %>
        Wrong answer penalty: <%= text_field_tag 'questionnaire[wrong_answer_penalty]', '', :disabled => true %>
        <span id=statistical_penalty_checkbox>
          Statistical penalty <input id="statistical_penalty" name="statistical_penalty" type="checkbox" checked=1
           value="1" onclick="toggle_and_set_wrong_penalty('questionnaire_wrong_answer_penalty');" />
        </span>
      <% else %>
        Wrong answer penalty: <%= f.text_field :wrong_answer_penalty %>
        <span id=statistical_penalty_checkbox>
          Statistical penalty <input id="statistical_penalty" name="statistical_penalty" type="checkbox" 
           value="1" onclick="toggle_and_set_wrong_penalty('questionnaire_wrong_answer_penalty');" />
        </span>
      <% end %>
    </p>
    <p> No answer penalty: <%= f.text_field :no_answer_penalty %> </p>
    <label for="select_questions">Questions:</label>
    <%= collection_multiple_select :questionnaire, :question_ids, @questions, :id, :wording,
                  {:outer_class => 'multiple_select', :inner_class => 'multiple_select_checkbox'} %>  
    <%= tree_multiple_select(:questionnaire, :category_ids, @root_categories, :id, :name)%>
    <p> <%= submit_tag 'Update questionnaire' %>    </p>
  <% end %>
</div>

<div id=links_section>
  <%= link_to 'Back to questionnaire page', questionnaire_path %>
  <%= link_to 'Back to \'List of Questionnaires\'', questionnaires_path %>
</div>


<script type="text/javascript">
function toggle_and_reset(id) {
  if (document.getElementById(id).disabled) {       // toggle
    document.getElementById(id).disabled = false;
  } else {
    document.getElementById(id).disabled = true;
  }
  document.getElementById(id).value = ""            // reset
}

function set_wrong_penalty_checkbox_disabled_properties(id) {
  backColor = document.getElementById(id).style.backgroundColor;
  if (backColor == "") { backColor = 'white'; }
  document.getElementById(id).style.color = backColor;
  document.getElementById(id).value = -1.0;
}

function toggle_and_set_wrong_penalty(id) {
  if (document.getElementById(id).disabled) {
    document.getElementById(id).disabled = false;
    document.getElementById(id).style.color = 'black';
    document.getElementById(id).value = 0.0
  } else {
    document.getElementById(id).disabled = true;
    set_wrong_penalty_checkbox_disabled_properties('questionnaire_wrong_answer_penalty');
  }
}
if (document.getElementById('questionnaire_wrong_answer_penalty').disabled) {
  set_wrong_penalty_checkbox_disabled_properties('questionnaire_wrong_answer_penalty');
} else {
  if (document.getElementById('statistical_penalty').checked) {
    toggle_and_set_wrong_penalty('questionnaire_wrong_answer_penalty');    
  }
}

</script>